<template>
  <div class="app-login">
    <div class="login-box">
      <h1>智城精治综合管理平台</h1>
      <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="0px" class="ruleForm-wrap">
        <el-form-item label prop="userName">
          <el-input v-model="ruleForm.userName" placeholder="用户名" maxlength="24"></el-input>
          <span class="show-icon iconfont icon-people"></span>
        </el-form-item>
        <el-form-item label prop="password">
          <el-input type="password" v-model="ruleForm.password" placeholder="密码" maxlength="24"></el-input>
          <span class="show-icon iconfont icon-lock"></span>
        </el-form-item>
        <el-form-item label prop="verifyCode">
          <el-input v-model="ruleForm.verifyCode" class="verify-code-input" placeholder="验证码" maxlength="4" @keyup.enter.native="submitForm('ruleForm')"></el-input>
          <span class="show-icon iconfont icon-check-circle"></span>
          <div class="code-img">
            <img @click="refreshIdentify" v-if="identifyUrl" :src="identifyUrl" />
          </div>
        </el-form-item>
        <el-row class="remember-password">
          <el-col :span="12">
            <el-checkbox v-model="checked">1天内免登录</el-checkbox>
          </el-col>
          <el-col :span="12">
            <el-form-item class="form-item-agreement" prop="agreementCk">
              <el-checkbox v-model="ruleForm.agreementCk"></el-checkbox>
              <span class="privacy" @click="handleChange">用户隐私协议</span>
            </el-form-item>
          </el-col>
        </el-row>

        <el-form-item>
          <el-button class="login-btn" type="primary" @click="submitForm('ruleForm')">登录</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="beian">沪ICP备19006540号-2</div>
    <loginMovieBg></loginMovieBg>
    <el-dialog title="平台隐私政策" :visible.sync="isAgreement" center :modal-append-to-body="false" top="5vh" width="60%">
      <div class="dialog-body">
        <Agreement></Agreement>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import qs from 'qs'
import loginMovieBg from '../../components/common/ui/loginMovieBg'
import SIdentify from '../../components/common/event/codeVerify'
import md5 from 'md5'
import Agreement from './agreement.vue'

export default {
  name: 'appLogin',
  components: {
    loginMovieBg,
    SIdentify,
    Agreement
  },
  data () {
    return {
      isAgreement: false,
      checked: false,
      ruleForm: {
        userName: '',
        password: '',
        verifyCode: '',
        agreementCk: []
      },
      sessionId: null,
      identifyUrl: null,
      rules: {
        userName: [
          { required: true, message: '请输入用户名', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' }
        ],
        verifyCode: [
          { required: true, message: '请输入验证码', trigger: 'blur' }
        ],
        agreementCk: [
          { required: true, message: '请勾选用户隐私协议' }
        ]
      }
    }
  },
  mounted () {
    let localToken = this.$base.getLocalStorage('localToken')
    let localUserInfo = this.$base.getLocalStorage('localUserInfo')

    if (localToken && localUserInfo) {
      this.$base.setSessionStorage('token', localToken)
      this.$base.setSessionStorage('userInfo', localUserInfo)
      let hasRequestLoginApiStatus = this.$base.getLocalStorage('hasRequestLoginApi')
      if (!hasRequestLoginApiStatus) {
        window.location.reload()
      }
      this.$base.setLocalStorage('hasRequestLoginApi', 'yes')
    }

    this.refreshIdentify()
  },
  methods: {
    handleChange () {
      this.isAgreement = true
    },
    // 登录操作
    loginApp: function () {
      let _this = this
      let obj = {
        account: _this.ruleForm.userName,
        password: `${this.$base.randomString(3)}${md5(_this.ruleForm.password)}${this.$base.randomString(3)}`,
        sessionId: this.sessionId,
        code: _this.ruleForm.verifyCode
      }

      // loading的配置项
      const loadingOpts = {
        lock: true,
        text: '拼命登录中...',
        spinner: 'el-icon-loading',
        background: 'rgba(0, 0, 0, 0.7)'
      }

      const url = this.$base.getUrl('pcLogin')
      let loading = _this.$loading(loadingOpts)

      this.$http({
        method: 'POST',
        url: url,
        data: qs.stringify(obj),
        withCredentials: true
      }).then(function (res) {
        loading.close()
        if (res.data.code !== 0) {
          // 错误信息弹窗
          _this.$message.error(res.data.msg)
          _this.$base.setSessionStorage('token', '')
          _this.refreshIdentify()
          return false
        }
        let data = res.data
        if (!data.result) {
          return false
        }
        _this.$base.setSessionStorage('token', data.result.token.token)
        _this.$base.setSessionStorage('userInfo', data.result.userInfo)
        _this.$base.setLocalStorage('hasRequestLoginApi', '')
        if (_this.checked) {
          _this.$base.setLocalStorage('localToken', data.result.token.token)
          _this.$base.setLocalStorage('localUserInfo', data.result.userInfo)
        }

        if (data.result.userInfo.chargeMenuFlag === '1') {
          // window.location.reload()
          _this.$router.push({ path: '/electromobile/index' })
          setTimeout(() => {
            window.location.reload()
          }, 500)
        } else {
          _this.$router.push({ path: '/dashboard' })
          setTimeout(() => {
            window.location.reload()
          }, 500)
        }
        // window.location.reload()
      })
    },

    refreshIdentify () {
      this.sessionId = this.$base.randomString()
      this.identifyUrl = this.$base.getUrl('getCheckCode') + '.jpg?sessionId=' + this.sessionId
    },

    submitForm (formName) {
      let _this = this
      this.$refs[formName].validate((valid) => {
        if (valid) {
          _this.loginApp()
        } else {
          console.log('登录失败!!')
          return false
        }
      })
    },
    resetForm (formName) {
      this.$refs[formName].resetFields()
    }

  }
}
</script>

<style lang="less" scoped>
.privacy {
  display: inline-block;
  padding-left: 10px;
  line-height: 20px;
  color: #409eff;
  &:hover {
    cursor: pointer;
  }
}
.form-item-agreement {
  margin-bottom: 0;
  /deep/.el-form-item__content {
    line-height: 0;
  }
}
.app-login {
  position: absolute;
  left: 0;
  top: 0;
  width: 100%;
  height: 100%;
  text-align: center;
  display: table;
  /* background: url(/static/images/background.png) #64a0e6 center no-repeat; */
  /* background-size: auto 100%; */
  overflow: hidden;
  background: #262b2e;
}

.login-box {
  position: absolute;
  z-index: 999;
  top: 50%;
  left: 50%;
  display: table-cell;
  vertical-align: middle;
  width: 360px;
  /* padding-top: 200px; */
  margin: 0 auto;
  transform: translate(-50%, -50%);
}

.login-box h1 {
  padding-left: 70px;
  color: #fff;
  font-size: 24px;
  text-align: center;
}

.login-box h1::before {
  content: '';
  position: absolute;
  top: -12px;
  left: 20px;
  // background: url(../../assets/images/logo-liantong.png) center center no-repeat;
  background-size: contain;
  width: 70px;
  height: 40px;
}

.login-form {
  color: #afafaf;
}

.ruleForm-wrap {
  margin-top: 30px;
  .el-form-item {
    position: relative;
    .show-icon {
      position: absolute;
      top: 0px;
      left: 10px;
      color: rgb(202, 202, 202);
    }
  }
  .el-form-item:nth-last-child(3) {
    margin-bottom: 14px !important;
  }
  /deep/ .el-form-item .el-input input {
    padding: 0 36px !important;
  }
}
.verify-code-input {
  width: 180px;
  float: left;
}
.code-img {
  width: 130px;
  height: 40px;
  margin-left: 10px;
  float: left;
  overflow: hidden;
  background-color: rgb(222, 222, 222);
  border-radius: 4px;
  cursor: pointer;
  img {
    transform: translate(-55px, -2px);
    width: 220px;
    height: 40px;
  }
}

.login-btn {
  width: 320px;
}

.login-icon {
  margin: 0 15px;
}

.remember-password {
  margin-bottom: 20px;
  text-align: left;
  padding-left: 10px;
  .el-checkbox {
    color: #cacaca !important;
  }
}

.beian {
  position: fixed;
  bottom: 20px;
  left: 50%;
  transform: translate(-72px, 0);
  color: #415b86;
}
</style>
